package com.xgimi.open.skill.datamanager.biz.utils;


import com.xgimi.open.skill.datamanager.service.request.SpeechRuleReqDTO;

import java.util.regex.Pattern;

/**
 * @author coderzpw.zhang
 * @version 1.0
 * @description 描述
 * @date 2023/2/26 13:51
 */
public class RegularPatternUtil {

    // 数字
    public static final String NUMBER = "^-?[0-9]+(\\.[0-9]+)?$";

    // ?!xx : 不能以xx开始
    // 不以 | 开头
    public static final String NOT_BEGINE_WITH_LINE = "(?!\\|)";
    // 不以 | 结尾
    public static final String NOT_END_WITH_LINE = "(?<!\\|)";
    // 数字、英文、中文
    public static final String CHINESE_NUMBER_ALPHABET = "[a-zA-Z0-9]+";
    // 除去【】[]() 的任意字符
    public static final String ANY_CHARACTER = "[^【】\\[\\]\\(\\)]+";
    // 数字、英文、中文、|
    public static final String CHINESE_NUMBER_ALPHABET_LINE_SPOT = "[\\|a-zA-Z0-9\\.+]+";
    //    public static final String SLOT_LEFT = "\\{";
//    public static final String SLOT_RIGHT = "\\}";
    public static final String SLOT_LEFT = "【";
    public static final String SLOT_RIGHT = "】";

    public static final String SLOT = SLOT_LEFT + ANY_CHARACTER + SLOT_RIGHT;
    // 数字、英文、中文、|、【slot】
    public static final String CHINESE_NUMBER_ALPHABET_LINE_SLOT = NOT_BEGINE_WITH_LINE + "[\\|a-zA-Z0-9\u4e00-\u9fa5(" + SLOT + ")]+" + NOT_END_WITH_LINE;
    public static final String LINE = "\\|?";
    public static final String MAY_LEFT = "\\[";
    public static final String MAY_RIGHT = "\\]";
    public static final String MUST_LEFT = "\\(";
    public static final String MUST_RIGHT = "\\)";

    /**
     * 判断字符串是否是数字
     *
     * @return
     */
    public static Boolean isNumber(String s) {
        return Pattern.compile(NUMBER).matcher(s).find();
    }

    /**
     * 校验是否符合说法规则
     *
     * @return
     */
    public static Boolean checkPattern(String text) {
        // 槽位
        String p1 = SLOT;
        // 可选
        String p2 = MAY_LEFT + NOT_BEGINE_WITH_LINE + ANY_CHARACTER + NOT_END_WITH_LINE + MAY_RIGHT;
        // 必选
        String p3 = MUST_LEFT + NOT_BEGINE_WITH_LINE + ANY_CHARACTER + NOT_END_WITH_LINE + MUST_RIGHT;

        String patternStr = "^(" + ANY_CHARACTER + "|" + p1 + "|" + p2 + "|" + p3 + ")+$";
        Pattern pattern = Pattern.compile(patternStr);
        return pattern.matcher(text).find();
    }

    public static Boolean checkSpeechRule(SpeechRuleReqDTO speechRuleReqDTO) {
        return checkPattern(speechRuleReqDTO.getRuleText());
    }
}
